package com.everest.tribalapp;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Desa_Form2 extends Activity implements View.OnClickListener
{

	private Button next;
	private Button prev;
	private TextView desa_f2_qes_4;
	private TextView desa_f2_qes_5;
	private TextView desa_f2_qes_6a;
	private TextView desa_f2_qes_6b;
	public String value="";
	private EditText desa_f2_ans_1;
	private EditText desa_f2_ans_2;
	private EditText desa_f2_ans_3;
	private EditText desa_f2_ans_4;
	private EditText desa_f2_ans_5;
	private EditText desa_f2_ans_6a;
	private EditText desa_f2_ans_6b;
	private CharSequence[] desa_f2_ans_4_values={"Laki-laki","Perempuan"};
	private CharSequence[] desa_f2_ans_5_values={"SD","SMP","SLTA","diploma keatas"}; 
	private CharSequence[] desa_f2_ans_6a_values={"Pengelola","Bukan Bukan Pengelola"};
	@Override
	public void onBackPressed() 
	{
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Desa_Form2.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.desa_f2);
		next=(Button)findViewById(R.id.desa_f2_next);
		prev=(Button)findViewById(R.id.desa_f2_prev);
		desa_f2_qes_4=(TextView)findViewById(R.id.desa_f2_qes_4);
		desa_f2_qes_5=(TextView)findViewById(R.id.desa_f2_qes_5);
		desa_f2_qes_6a=(TextView)findViewById(R.id.desa_f2_qes_6a);
		desa_f2_qes_6b=(TextView)findViewById(R.id.desa_f2_qes_6b);
		
		desa_f2_ans_1=(EditText)findViewById(R.id.desa_f2_ans_1);
		desa_f2_ans_2=(EditText)findViewById(R.id.desa_f2_ans_2);
		desa_f2_ans_3=(EditText)findViewById(R.id.desa_f2_ans_3);
		desa_f2_ans_4=(EditText)findViewById(R.id.desa_f2_ans_4);
		desa_f2_ans_5=(EditText)findViewById(R.id.desa_f2_ans_5);
		desa_f2_ans_6a=(EditText)findViewById(R.id.desa_f2_ans_6a);
		desa_f2_ans_6b=(EditText)findViewById(R.id.desa_f2_ans_6b);
		
		desa_f2_ans_4.setFocusable(false);
		desa_f2_ans_5.setFocusable(false);
		desa_f2_ans_6a.setFocusable(false);
		desa_f2_qes_4.setOnClickListener(this);
		desa_f2_qes_5.setOnClickListener(this);
		desa_f2_qes_6a.setOnClickListener(this);
		desa_f2_qes_6b.setOnClickListener(this);
		desa_f2_ans_4.setOnClickListener(this);
		desa_f2_ans_5.setOnClickListener(this);
		desa_f2_ans_6a.setOnClickListener(this);
		next.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				Intent intent=new Intent(getApplicationContext(),Desa_Form3.class);
				startActivity(intent);
				Desa_Form2.this.finish();
				
			}
		});
		prev.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				Intent intent=new Intent(getApplicationContext(),Desa_Form1.class);
				startActivity(intent);
				Desa_Form2.this.finish();
				
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==desa_f2_qes_4.getId())
		{
			value=getResources().getString(R.string.pinter_q2_4);
			toast_msg();
		}
		else if(v.getId()==desa_f2_qes_5.getId())
		{
			value=getResources().getString(R.string.pinter_q2_5);
			toast_msg();
		}
		else if(v.getId()==desa_f2_qes_6a.getId())
		{
			value=getResources().getString(R.string.pinter_q2_6a);
			toast_msg();
		}
		else if(v.getId()==desa_f2_qes_6b.getId())
		{
			value=getResources().getString(R.string.pinter_q2_6b);
			toast_msg();
		}
		else if(v.getId()==desa_f2_ans_6a.getId())
		{
			msg_f2_ans_6a();
		}
		else if(v.getId()==desa_f2_ans_5.getId())
		{
			msg_f2_ans_5();
		}
		else if(v.getId()==desa_f2_ans_4.getId())
		{
			msg_f2_ans_4();
		}
		
	}
	
	public void toast_msg()
	{
		LayoutInflater inflater = getLayoutInflater();

		View layout = inflater.inflate(R.layout.custom_toast,
				(ViewGroup) findViewById(R.id.custom_toast_layout_id));
		TextView text = (TextView) layout.findViewById(R.id.text);
		text.setText(value);
		Toast toast = new Toast(getApplicationContext());
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(layout);
		toast.show();
	}
	
	private void msg_f2_ans_4() {
	     Builder builder = new AlertDialog.Builder(this);
	     builder.setTitle("Select");
	     builder.setItems(desa_f2_ans_4_values, new DialogInterface.OnClickListener() {
	       @Override
	       public void onClick(DialogInterface dialog, int which) {
	        desa_f2_ans_4.setText(""+(which+1));
	         dialog.dismiss();
	       }
	     });
	     builder.setNegativeButton("cancel",
	         new DialogInterface.OnClickListener() {
	           @Override
	           public void onClick(DialogInterface dialog, int which) {
	             dialog.dismiss();
	           }
	         });
	     AlertDialog alert = builder.create();
	     alert.show();
	   }
	
	private void msg_f2_ans_5() {
	     Builder builder = new AlertDialog.Builder(this);
	     builder.setTitle("Select");
	     builder.setItems(desa_f2_ans_5_values, new DialogInterface.OnClickListener() {
	       @Override
	       public void onClick(DialogInterface dialog, int which) {
	        desa_f2_ans_5.setText(""+(which+1));
	         dialog.dismiss();
	       }
	     });
	     builder.setNegativeButton("cancel",
	         new DialogInterface.OnClickListener() {
	           @Override
	           public void onClick(DialogInterface dialog, int which) {
	             dialog.dismiss();
	           }
	         });
	     AlertDialog alert = builder.create();
	     alert.show();
	   }
	
	private void msg_f2_ans_6a() {
	     Builder builder = new AlertDialog.Builder(this);
	     builder.setTitle("Select");
	     builder.setItems(desa_f2_ans_6a_values, new DialogInterface.OnClickListener() {
	       @Override
	       public void onClick(DialogInterface dialog, int which) {
	        desa_f2_ans_6a.setText(""+(which+1));
	         dialog.dismiss();
	       }
	     });
	     builder.setNegativeButton("cancel",
	         new DialogInterface.OnClickListener() {
	           @Override
	           public void onClick(DialogInterface dialog, int which) {
	             dialog.dismiss();
	           }
	         });
	     AlertDialog alert = builder.create();
	     alert.show();
	   }
	

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		SharedPreferences preferences=getSharedPreferences("desa_f2_details",MODE_PRIVATE);
		SharedPreferences.Editor editor=preferences.edit();
		editor.putString("2_1", desa_f2_ans_1.getText().toString().trim());
		editor.putString("2_2", desa_f2_ans_2.getText().toString().trim());
		editor.putString("2_3", desa_f2_ans_3.getText().toString().trim());
		editor.putString("2_4", desa_f2_ans_4.getText().toString().trim());
		editor.putString("2_5", desa_f2_ans_5.getText().toString().trim());
		editor.putString("2_6a", desa_f2_ans_6a.getText().toString().trim());
		editor.putString("2_6b", desa_f2_ans_6b.getText().toString().trim());
		editor.commit();
		
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences preferences=getSharedPreferences("desa_f2_details",MODE_PRIVATE);
		desa_f2_ans_1.setText(preferences.getString("2_1", ""));
		desa_f2_ans_2.setText(preferences.getString("2_2", ""));
		desa_f2_ans_3.setText(preferences.getString("2_3", ""));
		desa_f2_ans_4.setText(preferences.getString("2_4", ""));
		desa_f2_ans_5.setText(preferences.getString("2_5", ""));
		desa_f2_ans_6a.setText(preferences.getString("2_6a", ""));
		desa_f2_ans_6b.setText(preferences.getString("2_6b", ""));
	}
}
